﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

using Spring.Context;
using Spring.Context.Support;

using Host.IContract;
using Host.ModelDTO;
using Server.IManager;
using Domain.Model;
using Host.DataTransfer;
using Domain.DomainBase;

namespace Host.Contract
{
    public class ServiceContract : IServiceContract
    {
        private static log4net.ILog logger = log4net.LogManager.GetLogger(typeof(ServiceContract));

        private IApplicationContext applicationContext = ContextRegistry.GetContext();

        public List<UserDTO> GetAllUser()
        {
            IUserManager manager = applicationContext.GetObject("UserManagerTrans") as IUserManager;
            try
            {
                List<User> list = manager.GetAll().ToList();
                List<UserDTO> result = list.Select(model => model.ToDTO<UserDTO>()).ToList();
                return result;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return new List<UserDTO>();
            }
        }

        public object SaveUser(UserDTO model)
        {
            IUserManager manager = applicationContext.GetObject("UserManagerTrans") as IUserManager;
            ICompanyManager companyManager = applicationContext.GetObject("CompanyManagerTrans") as ICompanyManager;
            IList<Func<IDomainEntity>> refEntitie = new List<Func<IDomainEntity>>() 
            { 
                () => companyManager.Get(model.CompanyID) 
            };
            try
            {
                User user = model.ToModel<User>(refEntitie);
                object result = manager.Save(user);
                return result;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return -1;
            }
        }

        public void UpdateUser(UserDTO model)
        {
            IUserManager manager = applicationContext.GetObject("UserManagerTrans") as IUserManager;
            ICompanyManager companyManager = applicationContext.GetObject("CompanyManagerTrans") as ICompanyManager;
            IList<Func<IDomainEntity>> refEntitie = new List<Func<IDomainEntity>>() 
            { 
                () => companyManager.Get(model.CompanyID) 
            };
            try
            {
                User user = model.ToModel<User>(() => manager.Get(model.UserID), refEntitie);
                manager.Update(user);
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }
        }

        public void DeleteUser(List<long> idList)
        {
            IUserManager manager = applicationContext.GetObject("UserManagerTrans") as IUserManager;
            try
            {
                manager.Delete(idList);
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }
        }

        public List<UserDTO> FindUserPageList(out long total, int page, int rp, string sortname, string sortorder, string qtype, string query)
        {
            IUserManager manager = applicationContext.GetObject("UserManagerTrans") as IUserManager;
            try
            {
                List<User> list = manager.FindPageList(out total, typeof(User).GetProperty(sortname), sortorder, (page - 1) * rp, rp).ToList();
                List<UserDTO> result = list.Select(model => model.ToDTO<UserDTO>()).ToList();
                return result;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                total = 0;
                return new List<UserDTO>();
            }
        }

        public List<CompanyDTO> FindCompanyPageList(out long total, int page, int rp, string sortname, string sortorder, string qtype, string query)
        {
            ICompanyManager manager = applicationContext.GetObject("CompanyManagerTrans") as ICompanyManager;
            try
            {
                List<Company> list = manager.FindPageList(out total, typeof(Company).GetProperty(sortname), sortorder, (page - 1) * rp, rp).ToList();
                List<CompanyDTO> result = list.Select(model => model.ToDTO<CompanyDTO>()).ToList();
                return result;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                total = 0;
                return new List<CompanyDTO>();
            }
        }
    }
}
